﻿@using viBook.Common
@using viBook.Models
@model IEnumerable<Category>
@{
    ViewBag.Title = "Categories";
    ViewBag.Menu = "Management";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
@(Html.Telerik().Grid(Model)
               .Name("category-data-grid")
               .ToolBar(commands => commands.Insert())
               .DataKeys(keys => keys.Add(c => c.Id))
               .Columns(columns =>
                            {
                                columns.Bound(category => category.CategoryName)
                                    .Template(@<text>
                                                   <a href='@Url.Action("Details","Categories", new {id = item.Id})'>@item.CategoryName</a>
                                               </text>
                                    )
                                    .ClientTemplate(string.Format("<a href='/Categories/Details/<#= Id #>'><#= CategoryName #></a>"))
                                    .Title("Category");
                                columns.Bound(category => category.NumberOfBook)
                                    .Title("Number of Books")
                                    .ReadOnly();
                                columns.Command(c =>
                                                    {
                                                        c.Edit();
                                                        c.Delete();
                                                    }).Width(200);
                            })
               .DataBinding(dataBinding =>
                                {
                                    dataBinding.Server().Select("Categories", "Admins");
                                    dataBinding.Ajax().Select("SelectCategoryAjax", "Admins").Enabled(true);
                                    dataBinding.Ajax().Insert("InsertCategoryAjax", "Admins").Enabled(true);
                                    dataBinding.Ajax().Update("UpdateCategoryAjax", "Admins").Enabled(true);
                                    dataBinding.Ajax().Delete("DeleteCategoryAjax", "Admins").Enabled(true);
                                })
                .ClientEvents(events => events.OnError("onError"))
                .Editable(editing => editing.Mode(GridEditMode.InLine))
                .Scrollable()
                .Sortable()
                .Pageable()
                .Footer(true)
        )
<script type="text/javascript">
    function onError(e) {
        if (e.textStatus == 'error') {
            if (e.XMLHttpRequest.status == "500") {
                alert(e.XMLHttpRequest.responseText);
                e.preventDefault();
            }
        }
    }

</script>